package com.javaweb.config.redis;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

import com.javaweb.base.BaseConfigMemory;
import com.javaweb.base.BaseService;
import com.javaweb.base.BaseSystemMemory;
import com.javaweb.base.po.Config;
import com.javaweb.base.po.Dictionary;
import com.javaweb.base.po.Interfaces;
import com.javaweb.constant.SystemConstant.RedisConstant;

/**
 * 消息接收
 * @author tank2140896
 */
@Component
public class RedisMqReceive extends BaseService implements MessageListener {

	public void onMessage(Message message,byte[] byteArray) {
		String channel = new String(message.getChannel());//频道名
		//String body = new String(message.getBody());//消息体
		//System.out.println("接收到的消息体为："+body);
		switch(channel) {
			case RedisConstant.REDIS_CONFIG_TOPIC -> updateConfigInfo(configService.selectAll());//更新配置表
			case RedisConstant.REDIS_DICTIONARY_TOPIC -> updateDictionaryInfo(dictionaryService.selectAll());//更新字典表
			case RedisConstant.REDIS_INTERFACES_TOPIC -> updateInterfacesInfo(interfacesService.getAll());//更新接口表
		}
	}
	
	public static void updateConfigInfo(List<Config> list){
		BaseSystemMemory.configList = list;
		BaseSystemMemory.configMap = BaseSystemMemory.configList.stream().collect(Collectors.toMap(Config::getKeyCode,e->e/*Function.identity()*/,(k1,k2)->k1));
		BaseConfigMemory.refresh();
	}
	
	public static void updateDictionaryInfo(List<Dictionary> list){
		BaseSystemMemory.dictionaryList = list;
		BaseSystemMemory.dictionaryMap = BaseSystemMemory.dictionaryList.stream().collect(Collectors.toMap(Dictionary::getKeyCode,e->e/*Function.identity()*/,(k1,k2)->k1));
	}
	
	public static void updateInterfacesInfo(List<Interfaces> list){
		BaseSystemMemory.interfacesList = list;
	}

}
